package com.ruoyi.ai.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruoyi.ai.domain.AiWorkCollect;
import com.ruoyi.ai.domain.AiWorkHistory;
import com.ruoyi.ai.mapper.AiWorkCollectMapper;
import com.ruoyi.ai.mapper.AiWorkHistoryMapper;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.annotation.DataUserScope;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ai.mapper.AiWorkMapper;
import com.ruoyi.ai.domain.AiWork;
import com.ruoyi.ai.service.IAiWorkService;

/**
 * 作品管理Service业务层处理
 *
 * @author tang
 * @date 2024-12-10
 */
@Service
public class AiWorkServiceImpl implements IAiWorkService
{
    @Autowired
    private AiWorkMapper aiWorkMapper;

    @Autowired
    private AiWorkCollectMapper aiWorkCollectMapper;

    @Autowired
    private AiWorkHistoryMapper aiWorkHistoryMapper;

    /**
     * 查询作品管理
     *
     * @param workId 作品管理主键
     * @return 作品管理
     */
    @Override
    public AiWork selectAiWorkByWorkId(Long workId)
    {
        AiWorkHistory aiWorkHistory=new AiWorkHistory();
        aiWorkHistory.setWorkId(workId);
        aiWorkHistory.setUserId(SecurityUtils.getUserId());
        insertAiWorkHistory(aiWorkHistory);
        return aiWorkMapper.selectAiWorkByWorkId(workId);
    }

    /**
     * 查询作品管理列表
     *
     * @param aiWork 作品管理
     * @return 作品管理
     */
    @Override
    @DataUserScope
    public List<AiWork> selectAiWorkList(AiWork aiWork)
    {
        return aiWorkMapper.selectAiWorkList(aiWork);
    }

    /**
     * 新增作品管理
     *
     * @param aiWork 作品管理
     * @return 结果
     */
    @Override
    public int insertAiWork(AiWork aiWork)
    {
        aiWork.setCreateTime(DateUtils.getNowDate());
        return aiWorkMapper.insertAiWork(aiWork);
    }

    /**
     * 修改作品管理
     *
     * @param aiWork 作品管理
     * @return 结果
     */
    @Override
    public int updateAiWork(AiWork aiWork)
    {
        aiWork.setUpdateTime(DateUtils.getNowDate());
        return aiWorkMapper.updateAiWork(aiWork);
    }

    /**
     * 批量删除作品管理
     *
     * @param workIds 需要删除的作品管理主键
     * @return 结果
     */
    @Override
    public int deleteAiWorkByWorkIds(Long[] workIds)
    {
        return aiWorkMapper.deleteAiWorkByWorkIds(workIds);
    }

    /**
     * 删除作品管理信息
     *
     * @param workId 作品管理主键
     * @return 结果
     */
    @Override
    public int deleteAiWorkByWorkId(Long workId)
    {
        return aiWorkMapper.deleteAiWorkByWorkId(workId);
    }

    @Override
    public List<AiWork> selectAiWorkListByCollect(AiWorkCollect aiWorkCollect) {
        QueryWrapper<AiWorkCollect> wrapper=new QueryWrapper<>();
        List<AiWork> aiWorkList=new ArrayList<>();
        if (ObjectUtils.isNotEmpty(aiWorkCollect)){
            Long userId = aiWorkCollect.getUserId();
            Date date = aiWorkCollect.getCreateDate();
            wrapper.eq("user_id", userId);
            if (ObjectUtils.isNotEmpty(date)){
                String createDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date);
                wrapper.eq("create_date", createDate);
            }
        }
        List<AiWorkCollect> aiWorkCollects = aiWorkCollectMapper.selectList(wrapper);
        for (AiWorkCollect workCollect : aiWorkCollects) {
            AiWork aiWork = aiWorkMapper.selectAiWorkByWorkId(workCollect.getWorkId());
            aiWorkList.add(aiWork);
        }
        return aiWorkList;
    }

    @Override
    public int insertAiWorkCollect(AiWorkCollect aiWorkCollect) {
        QueryWrapper<AiWorkCollect> wrapper=new QueryWrapper<>();
        wrapper.eq("work_id", aiWorkCollect.getWorkId()).
                eq("user_id",aiWorkCollect.getUserId());
        List<AiWorkCollect> aiWorkCollects = aiWorkCollectMapper.selectList(wrapper);
        if (aiWorkCollects.isEmpty()){
            int i = aiWorkCollectMapper.insert(aiWorkCollect);
            if (i>0){
                AiWork aiWork=new AiWork();
                aiWork.setWorkId(aiWorkCollect.getWorkId());
                aiWork.setCollectNumber(1L);
                aiWorkMapper.updateAiWorkNumber(aiWork);
            }
            return i;
        }
        return 0;
    }

    @Override
    public int deleteAiWorkCollect(AiWorkCollect aiWorkCollect) {
        QueryWrapper<AiWorkCollect> wrapper=new QueryWrapper<>();
        wrapper.eq("work_id", aiWorkCollect.getWorkId())
                .eq("user_id",aiWorkCollect.getUserId());
        int i = aiWorkCollectMapper.delete(wrapper);
        if (i>0){
            AiWork aiWork=new AiWork();
            aiWork.setWorkId(aiWorkCollect.getWorkId());
            aiWork.setCollectNumber(-1L);
            aiWorkMapper.updateAiWorkNumber(aiWork);
        }
        return i;
    }

    @Override
    public List<AiWork> selectAiWorkListByHistory(AiWorkHistory aiWorkHistory) {
        QueryWrapper<AiWorkHistory> wrapper=new QueryWrapper<>();
        List<AiWork> aiWorkList=new ArrayList<>();
        Date date = aiWorkHistory.getCreateDate();
        wrapper.eq("user_id", aiWorkHistory.getUserId());
        if (ObjectUtils.isNotEmpty(date)){
            String createDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date);
            wrapper.eq("create_date", createDate);
        }
        List<AiWorkHistory> aiWorkHistories = aiWorkHistoryMapper.selectList(wrapper);
        for (AiWorkHistory workHistory : aiWorkHistories) {
            AiWork aiWork = aiWorkMapper.selectAiWorkByWorkId(workHistory.getWorkId());
            aiWorkList.add(aiWork);
        }
        return aiWorkList;
    }

    @Override
    public int insertAiWorkHistory(AiWorkHistory aiWorkHistory) {
        int i=0;
        QueryWrapper<AiWorkHistory> wrapper=new QueryWrapper<>();
        wrapper.eq("work_id", aiWorkHistory.getWorkId()).
                eq("user_id",aiWorkHistory.getUserId());
        List<AiWorkHistory> aiWorkHistories = aiWorkHistoryMapper.selectList(wrapper);
        if (aiWorkHistories.isEmpty()){
            i = aiWorkHistoryMapper.insert(aiWorkHistory);
            if (i>0){
                AiWork aiWork=new AiWork();
                aiWork.setWorkId(aiWorkHistory.getWorkId());
                aiWork.setUserNumber(1L);
                aiWorkMapper.updateAiWorkNumber(aiWork);
                return i;
            }
        }else {
            aiWorkHistory.setCreateDate(DateUtils.getNowDate());
            return aiWorkHistoryMapper.update(aiWorkHistory,wrapper);
        }
        return i;
    }

    @Override
    public int deleteAiWorkHistory(AiWorkHistory aiWorkHistory) {
        QueryWrapper<AiWorkHistory> wrapper=new QueryWrapper<>();
        wrapper.eq("work_id", aiWorkHistory.getWorkId()).
                eq("user_id",aiWorkHistory.getUserId());
        return aiWorkHistoryMapper.delete(wrapper);
    }

    @Override
    public int updateAiWorkLikeNumber(Long workId, Long number) {
        AiWork aiWork=new AiWork();
        aiWork.setWorkId(workId);
        aiWork.setLikeNumber(number);
        return aiWorkMapper.updateAiWorkNumber(aiWork);
    }
}
